import config from '@/config.js'
import * as util from '@/common/js/util'
import * as authUserApi from '@/interface/auth/authUserApi.js'
export default {
	state: {
		lat: '',
		lng: '',
		token: '',
		phone: '',
		userId: '',
		userName: '',
		avatarUrl: '',
		isExpired: false// 是否登录过期
	},
	getters: {
		hasLogin(state){
			return !!state.token;
		}
	},
	mutations: {
		//更新state数据
		setStateAttr(state, param){
			if(param instanceof Array){
				for(let item of param){
					state[item.key] = item.val;
				}
			}else{
				for(let key in param){
					state[key] = param[key];
				}
			}
		},
		//退出登录
		logout(state){
			uni.removeStorageSync('token');
			uni.removeStorageSync('userInfo');
			setTimeout(()=>{
				state.token = ''
				state.phone = ''
				state.userId = '';
				state.userName = '';
				state.avatarUrl = '';
				state.lat = '';
				state.lng = '';
			}, 200)
		}
	},
	actions: {
		async userLogout ({state, commit}){
			authUserApi.userLogout().then((res) => {
				commit('logout');
				util.showToast(res.message)
				// setTimeout(function() {
				// 	uni.navigateTo({
				// 		url: '/pages/login/login'
				// 	})
				// }, 500)
			}).catch(e=>{
				console.log(e)
			})
		},
		updateUserInfo ({state, commit}, data) {
			let type = data.type
			let userInfo = uni.getStorageSync('userInfo')
			if (userInfo) {
				if (type == 'user') {
					userInfo.status = data.status
					userInfo.nickName = data.userName
					userInfo.avatarUrl = data.avatarUrl
				}else if (type == 'auth') {
					userInfo.authStatus = data.authStatus
				}else {
					userInfo.phone = data.phone
				}
				uni.setStorageSync('userInfo', userInfo)
			}
			delete data.type
			if (data.hasOwnProperty('status')) delete data.status
			commit('setStateAttr', data)
		}
	}
}